<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of CompanyController
 *
 * @author simon.zwicker
 */
class CompanyController extends Zend_Controller_Action
{
    public function init()
    {
        if ($_SESSION['logged'] != TRUE) {
            header('location: /index/login');
        }
        parent::init();
    }
    
    protected function getCreateForm()
    {
       $form = new Zend_Form;
       $form->setAction('/company/checkcompany')->setMethod('post');
       
       $name = new Zend_Form_Element_Text('company_name',array('placeholder' => 'Firmenname'));
       $address = new Zend_Form_Element_Text('company_address',array('placeholder' => 'Anschrift'));
       $plz = new Zend_Form_Element_Text('plz',array('placeholder' => 'Postleitzahl'));
       $city = new Zend_Form_Element_Text('city',array('placeholder' => 'Ort'));
       $phone = new Zend_Form_Element_Text('phone',array('placeholder' => 'Telefonnummer'));
       $breaktime = new Zend_Form_Element_Text('breaktime',array('placeholder' => 'Pause (z.B. 12:00:00)'));
       
       $form->addElement($name)
           ->addElement($address)
           ->addElement($plz)
           ->addElement($city)
           ->addElement($phone)
           ->addElement($breaktime)
           ->addElement('submit','company_create',array('label' => 'Firma erstellen'));
       
       return $form;
    }
    
    protected function getUserInviteForm()
    {
        $form = new Zend_Form;
        $form->setAction('/company/checkuser')->setMethod('post');
        
        $email = new Zend_Form_Element_Text('email',array('placeholder' => 'E-Mail Adresse'));
        $email->addValidator('alnum')
              ->addValidator('regex', false, array('/^[a-z]/'))
              ->setRequired(true)
              ->addFilter('StringToLower');
        
        $form->addElement($email)->addElement('submit','UserLoad',array('label' => 'Benutzer einladen'));
        
        return $form;
    }

    public function companyAction()
    {
        if ($_SESSION['companyshow'] != 1) {
            header('location: /index/index');
        }
        $this->view->form = $this->getCreateForm();
    }
    
    public function userAction()
    {
        if ($_SESSION['companyshow'] == 1) {
            header('location: /index/index');
        }
        $this->view->form = $this->getUserInviteForm();
    }
    
    public function checkcompanyAction()
    {
        //DB
        $config = new Zend_Config_Ini(APPLICATION_PATH . '/configs/application.ini','staging');
        Zend_Registry::set('config',$config);

        $db = Zend_Db::factory($config->db);
        Zend_Registry::set('db',$db);
        
        $comp_param = $this->getRequest()->getParams();
        if (!empty($comp_param['company_name']) && !empty($comp_param['company_address']) && !empty($comp_param['plz']) && !empty($comp_param['city']) && !empty($comp_param['phone']) && !empty($comp_param['breaktime'])) {
            $data = $db->fetchAll('SELECT * FROM adresse WHERE name = "' . $comp_param['company_name'] . '"');
            if (!$data) {
                $db->insert('adresse', array(
                   'name' => $comp_param['company_name'],
                   'address' => $comp_param['company_address'],
                   'plz' => $comp_param['plz'],
                   'city' => $comp_param['city'],
                   'phone' => $comp_param['phone']
                ));

                $data = $db->fetchAll('SELECT * FROM adresse WHERE name = "' . $comp_param['company_name'] . '"');

                $db->insert('firma',array(
                    'address_id' => $data[0]['address_id'],
                    'pause' => $comp_param['breaktime'],
                    'create_id' => $_SESSION['user_id']
                ));

                $_SESSION['firma'] = $data[0]['name'];
                $_SESSION['companyshow'] = 0;

                $data = $db->fetchAll('SELECT * FROM firma WHERE create_id = ' . $_SESSION['user_id'] . '');

                $db->insert('userfirma', array(
                    'user_id' => $_SESSION['user_id'],
                    'firma_id' => $data[0]['firma_id'],
                    'status' => 0
                ));
                
                $_SESSION['my_firma'] = $data[0]['firma_id'];
                
                $this->view->msgcompany = 'Die folgende Firma wurde soeben angelegt:<br /><br />
                        
                    <b>Firmenname: </b>'. $comp_param['company_name'] .'<br />
                    <b>Anschrift: </b>'. $comp_param['company_address'] .'<br />
                    <b>PLZ & Ort: </b>'. $comp_param['plz'] .' '. $comp_param['city'] .'<br />
                    <b>Telefonnr.: </b>'. $comp_param['phone'] .'<br />
                    <b>Pause: </b>'. $comp_param['breaktime'] .'<br /><br />
                    
                    <h3>Gegründet von:</h3>
                    <b>Name: </b>'. $_SESSION['vorname'] .' '. $_SESSION['name'] .'<br />
                    <b>E-Mail: </b>'. $_SESSION['email'] .'<br />
                     ';
                
            } else {
                $this->view->msgcompany = 'Diese Firma ist schon angelegt! <br /><br /><a href="javascript:back()">Zurück</a>';
            }
            
        } else {
            $this->view->msgcompany = 'Bitte alle Felder ausfüllen! <br /><br /><a href="javascript:back()">zurück</a>';
        }
    }
    
    public function checkuserAction()
    {
        //DB
        $config = new Zend_Config_Ini(APPLICATION_PATH . '/configs/application.ini','staging');
        Zend_Registry::set('config',$config);

        $db = Zend_Db::factory($config->db);
        Zend_Registry::set('db',$db);
        
        $invite_param = $this->getRequest()->getParams();
        
        $sql = 'SELECT * FROM user u, userfirma uf WHERE u.email = "' . $invite_param['email'] . '" AND u.user_id = uf.user_id';
        $data = $db->fetchAll($sql);
        
        if (!$data) {
            $data = $db->fetchAll('SELECT * FROM user WHERE email = "' . $invite_param['email'] . '"');
            $db->insert('userfirma', array(
                'user_id' => $data[0]['user_id'],
                'firma_id' => $_SESSION['my_firma'],
                'status' => 1
            ));
            $this->view->msguser = 'Der Benutzer mit der Email: '. $invite_param['email'] .' wurde zur Ihrer Firma eingeladen!';
        } else {
            $this->view->msguser = 'Dieser Benutzer ist entweder nicht vorhanden oder wurde bereits zu einer Firma eingeladen!';
        }
    }
    
    public function acceptAction()
    {
        //DB
        $config = new Zend_Config_Ini(APPLICATION_PATH . '/configs/application.ini','staging');
        Zend_Registry::set('config',$config);

        $db = Zend_Db::factory($config->db);
        Zend_Registry::set('db',$db);
        
        $db->update('userfirma', array(
            'status' => 0
        ), 'user_id =' . $_SESSION['user_id']);
        
        $_SESSION['firma'] = $_SESSION['loadname'];
        $_SESSION['loadname'] = NULL;
        $_SESSION['load'] = NULL;
        
        $this->view->accept = 'Sie gehören nun zur Firma: <b>' . $_SESSION['firma'] .'</b>';
    }
    
    public function dontacceptAction()
    {
        //DB
        $config = new Zend_Config_Ini(APPLICATION_PATH . '/configs/application.ini','staging');
        Zend_Registry::set('config',$config);

        $db = Zend_Db::factory($config->db);
        Zend_Registry::set('db',$db);
        
        $db->delete('userfirma', 'user_id =' . $_SESSION['user_id']);
        
        $this->view->dont = 'Sie haben die Einladung der Firma: <b>' . $_SESSION['loadname'] .'</b> abgelehnt und können von einer anderen Firma eingeladen werden oder selbst eine Firma anlegen!';
        
        $_SESSION['loadname'] = NULL;
        $_SESSION['load'] = NULL;
        $_SESSION['companyshow'] = 1;
    }
}

